package com.laizhenghua.payment.controller;

import com.laizhenghua.commons.entity.CommonResult;
import com.laizhenghua.commons.entity.PaymentEntity;
import com.laizhenghua.payment.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @date: 2022/1/1 18:24
 */
@Slf4j
@RestController
@RequestMapping(value = "/payment")
public class PaymentController {
    @Autowired
    private PaymentService paymentService;
    @Value("${server.port}")
    private String serverPort;
    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<Integer> create(@RequestBody PaymentEntity entity) {
        Integer affectedRows = paymentService.create(entity);
        if (affectedRows > 0) {
            return new CommonResult<Integer>(200, "插入成功！serverPort：" + serverPort, affectedRows);
        }
        return new CommonResult<Integer>(444, "插入失败！", null);
    }

    @RequestMapping(value = "/getPaymentById/{id}", method = RequestMethod.GET)
    public CommonResult<PaymentEntity> getPaymentById(@PathVariable(value = "id", required = true) Integer id) {
        PaymentEntity entity = paymentService.getPaymentById(id);
        return new CommonResult<>(200, "查询成功！serverPort：" + serverPort, entity);
    }

    @RequestMapping(value = "/discovery", method = RequestMethod.GET)
    public Object discovery() {
        return this.discoveryClient;
    }
}
